﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.Common.Public.Enums;
using System.Threading;
using RFIDStockManagement.BLL;

namespace RFIDStockManagement.Client
{
    public partial class StockList : XtraUserControl
    {
        public StockList()
        {
            InitializeComponent();
        }

        private void StockList_Load(object sender, EventArgs e)
        {
            FormSizeChange();
            BindGridData();
        }

        private void StockList_SizeChanged(object sender, EventArgs e)
        {
            FormSizeChange();
        }
        private void FormSizeChange()
        {
            gridData.Height = this.Height - (pnlSearch.Location.Y + pnlSearch.Height) + 3;
        }

        private void BindGridData()
        {
            gridView.ShowLoadingPanel();
            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state)
            {
                try
                {
                    Dictionary<string, object> filter = new Dictionary<string, object>();
                    if (txtName.Text.Trim().Length > 0)
                    {
                        filter.Add("FName", txtName.Text.Trim());
                    }
                    if (txtNumber.Text.Trim().Length > 0)
                    {
                        filter.Add("FNumber", txtNumber.Text.Trim());
                    }
                    DataTable dt = null;
                    if (filter.Count > 0)
                    {
                        dt = BasicInfoManager.Search(filter, EnumBasicInfo.Stock);
                    }
                    else
                    {
                        dt = BasicInfoManager.GetList(filter, EnumBasicInfo.Stock);
                    }
                    BindGridDataDelegate delegateMothod = new BindGridDataDelegate(BindGridDataHandler);
                    this.Invoke(delegateMothod, dt);
                }
                finally
                {
                    gridView.HideLoadingPanel();
                }
            }));
        }
        private delegate void BindGridDataDelegate(DataTable dt);
        private void BindGridDataHandler(DataTable dt)
        {
            gridData.DataSource = dt;
        }

        private void barBtnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                StockEdit form = new StockEdit(0);
                form.ShowDialog();
                barBtnRefresh_ItemClick(null, null);
            }
            catch (Exception ex)
            {
                AlertForm.ShowMessage(ex.Message);
            }
        }

        private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                int[] selectRows = gridView.GetSelectedRows();
                if (selectRows != null && selectRows.Length > 0)
                {
                    DataRowView rowView = gridView.GetRow(selectRows[0]) as DataRowView;
                    if (rowView != null)
                    {
                        long id = long.Parse(rowView.Row["FID"].ToString());
                        StockEdit editForm = new StockEdit(id);
                        editForm.ShowDialog();
                        BindGridData();
                    }
                }
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }

        private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                int[] selectRows = gridView.GetSelectedRows();
                if (selectRows == null || selectRows.Length == 0)
                {
                    AlertForm.ShowMessage("至少要选择一行才能删除");
                    return;
                }
                if (AlertForm.Confirm("确定删除所有选择的数据？"))
                {
                    List<string> lstId = new List<string>();
                    foreach (int iRow in selectRows)
                    {
                        DataRowView rowView = gridView.GetRow(iRow) as DataRowView;
                        if (rowView != null)
                        {
                            lstId.Add(rowView.Row["FID"].ToString());
                        }
                    }
                    if (lstId.Count > 0)
                    {
                        List<string> result = BasicInfoManager.Delete(lstId, EnumBasicInfo.Stock);
                        if (result.Count == 1)
                        {
                            AlertForm.ShowMessage(result[0]);
                        }
                        else
                        {
                            AlertForm.ShowMessage(result);
                        }
                        BindGridData();
                    }
                }
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }

        private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            txtName.Text = string.Empty;
            txtNumber.Text = string.Empty;
            BindGridData();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchData();
        }
        private void SearchData()
        {
            if (txtName.Text.Trim().Length == 0 && txtNumber.Text.Trim().Length == 0)
            {
                AlertForm.ShowMessage("请输入名称或编码再搜索");
                return;
            }
            BindGridData();
        }
        private void txtName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchData();
            }
        }
        private void txtNumber_KeyUp(object sender, KeyEventArgs e)
        {
            txtName_KeyUp(sender, e);
        }

        private void gridView_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "FType")
            {
                int iValue = 2;
                int.TryParse(e.Value.ToString(), out iValue);
                EnumStockType stockType = (EnumStockType)iValue;
                switch (stockType)
                {
                    case EnumStockType.Defective:
                        e.DisplayText = "不良品仓";
                        break;
                    case EnumStockType.Normal:
                        e.DisplayText = "正常品仓";
                        break;
                    case EnumStockType.Other:
                        e.DisplayText = "其它仓";
                        break;
                    case EnumStockType.Scrapped:
                        e.DisplayText = "报废品仓";
                        break;
                    default:
                        e.DisplayText = "正常品仓";
                        break;
                }
            }
        }















    }
}
